/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.persistence.PostpPersistence;

import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Observable;
import java.util.Observer;
import java.util.StringTokenizer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import br.ufmg.dees.insane.model.postp.Boundary;
import br.ufmg.dees.insane.model.postp.Patch;
import br.ufmg.dees.insane.model.postp.PointModel;
import br.ufmg.dees.insane.model.postp.StepPostp;
import br.ufmg.dees.insane.parser.FemModelToPostpModel;
import br.ufmg.dees.insane.persistence.Persistence;
import br.ufmg.dees.insane.util.IPoint3d;

/** A class representing postpPersistenceAsXML
 * @author Samuel Silva Penna
 * @since 27/08/2005
 */
public class ReaderFemXMLPersistence implements Persistence, Observer {

	 /**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;

	/** The Model of this persistence. */
	protected static StepPostp stepPostp;
	
	/** The Object which will be serialized. */
	protected static Object docOutput;
	
	private boolean isValid = true;
  
	/** Returns the Model of this application.
	*@return The Model of this application.
	*/
	public StepPostp getStep(){
		return (stepPostp);
	}
	
	/** Sets the Model of this application.
	*@param a The Model of this application.
	*/
	public void setStepPostp(StepPostp s){
		stepPostp = s;
	}
	
  /* (non-Javadoc)
   * @see br.ufmg.dees.insane.persistence.Persistence#fillModelfromFile(java.lang.String)
   */
  public void fillInsaneFromFile(String File) {
      
  	//Creating and configuring the SAXParser
  	SAXParserFactory parserFac = SAXParserFactory.newInstance();
  	
  	parserFac.setValidating(true);
  	
  	try
  	{
  		SAXParser parser = parserFac.newSAXParser();
  		
  		//implementing DefaultHandler
  		parser.parse(File, new DefaultHandler()
  		{
  			StringBuffer content = new StringBuffer();

  			//All the classes which will be instantiated are declared here
  			ArrayList<PointModel> pointsList;
  			ArrayList<Patch> patchesList;
  			ArrayList<Boundary> boundariesList;
  			PointModel point;
  			Boundary boundary;
  			Patch patch;
  			IPoint3d coord;
  			ArrayList<String> dofLabels;
  			
  			public void setDocumentLocator(Locator locator){
  			}
  			 
  			public void characters(char ch[], int start, int length){
  				this.content.append(ch, start, length);
  			}
  			
  			//todos os ifs deste metodo serao ativados quando o parser encontrar <....>
  			public void startElement(String uri, String localName, String qName, Attributes attributes)
  			{
  				if ("Insane".equals(qName)){
  					stepPostp = new StepPostp();
  				}
  				
  				else if ("Solution".equals(qName)){
  				}
  				
  				else if ("NumMaxSteps".equals(qName)){
  				}
  				
  				else if ("Step".equals(qName)){
  					
  				}
  				
  				else if ("NumMaxIterations".equals(qName)){
  				}
  				
  				else if ("Tolerance".equals(qName)){
  				}
  				
  				else if ("ConvergenceType".equals(qName)){
  				}
  				
  				else if ("IteractiveStrategyList".equals(qName)){
  				}
  				
  				else if ("IteractiveStrategy".equals(qName)){
  				}
  				
  				else if ("NodeControl".equals(qName)){
  				}
  				
  				else if ("DirectionControl".equals(qName)){
  				}
  				
  				
  				else if ("ChangeLoadFactor".equals(qName)){
  				}
  				
  				else if ("ChangeTolerance".equals(qName)){
  				}
  				
  				else if ("ChangeNumMaxIterations".equals(qName)){
  				}
  				
  				else if ("StepNumber".equals(qName)){
  					
  				}
  				
  				else if ("FinalLoadFactor".equals(qName)){
  				}
  				
  				
  				else if ("Model".equals(qName)){
  				}
  				
  				else if ("ProblemDriver".equals(qName)){
  				}
  				
  				else if ("GlobalAnalysisModel".equals(qName)){
  				}
  				
  				else if ("MaterialList".equals(qName)){
  				}
  				
  				else if ("Material".equals(qName)){
  				}
  				
  				else if ("Poisson".equals(qName)){
  				}
  				
  				else if ("Fc".equals(qName)){
  				}
  				
  				else if ("Ft".equals(qName)){
  				}
  				
  				else if ("ec".equals(qName)){
  				}
  				
  				else if ("et".equals(qName)){
  				}
  				
  				
  				else if ("E0".equals(qName)){
  				}
  				
  				else if ("Elasticity".equals(qName)){
  				}
  				
  				else if ("ShearModulus".equals(qName)){
  				}
  				
  				else if ("Yielding".equals(qName)){
  				}
  				
  				else if ("NodeList".equals(qName)){
  					pointsList = new ArrayList<PointModel>();
  				}
  				
  				else if ("Node".equals(qName)){
  					point = new PointModel();
  					point.setLabel(attributes.getValue("label"));
  				}
  				
  				else if ("NodeValues".equals(qName)){
  				}
  				
  				else if ("Coord".equals(qName)){
  					coord = new IPoint3d();
  				}
  				
  				else if ("Restraints".equals(qName)){
  				}
  				
  				else if ("DOFLabels".equals(qName)){
  					dofLabels = new ArrayList<String>();
  				}
  				
  				else if ("Displacements".equals(qName)){
  				}
  				
  				else if ("ElementList".equals(qName)){
  					patchesList = new ArrayList<Patch>();
  				}
  				
  				else if ("Element".equals(qName)){
  					patch = new Patch();
                    patch.setLabel(attributes.getValue("label"));
  				}
  				
  				else if ("Incidence".equals(qName)){
  				}
  				
  				else if ("AnalysisModel".equals(qName)){
  				}
  				
  				else if ("IntegrationOrder".equals(qName)){
  				}
  				
  				else if ("ElmCrossSections".equals(qName)){
  				}
  				
  				else if ("DegenerationList".equals(qName)){
  				}
  				
  				else if ("Degeneration".equals(qName)){
  				}
  				
  				else if ("GeneralizedStrains".equals(qName)){
  				}
  				
  				else if ("GeneralizedStress".equals(qName)){
  				}
  				
  				else if ("SectionPoint".equals(qName)){
  				}
  				
  				else if ("PointStrains".equals(qName)){
  				}
  				
  				else if ("PointStress".equals(qName)){
  				}
  				
  			} // END OF STAR ELEMENT*********************************************
	    			
  			//    todos os ifs deste metodo serao ativados quando o parser encontrar </....>
  			public void endElement(String uri, String localName, String qName)
  			{
  				
  				if ("Insane".equals(qName)){
  				//	stepPostp.setPoints(pointsList);
 				//	stepPostp.setBoundaries(boundariesList);
 				//	stepPostp.setPatches(patchesList);
  				}
  				
  				else if ("Solution".equals(qName)){
  				}
  				
  				else if ("NumMaxSteps".equals(qName)){
  					StringTokenizer stk = new StringTokenizer(content.toString());
  					stepPostp.putStepValue("NumMaxSteps",new Double(Double.parseDouble(stk.nextToken())));
  				}
  				
  				else if ("Step".equals(qName)){
  					
  				}
  				
  				else if ("NumMaxIterations".equals(qName)){
  					StringTokenizer stk = new StringTokenizer(content.toString());
  					stepPostp.putStepValue("NumMaxIterations",new Double(Double.parseDouble(stk.nextToken())));
  				}
  				
  				else if ("Tolerance".equals(qName)){
  				}
  				
  				else if ("ConvergenceType".equals(qName)){
  				}
  				
  				else if ("IteractiveStrategyList".equals(qName)){
  				}
  				
  				else if ("IteractiveStrategy".equals(qName)){
  				}
  				
  				else if ("NodeControl".equals(qName)){
  				}
  				
  				else if ("DirectionControl".equals(qName)){
  				}
  				
  				else if ("ChangeLoadFactor".equals(qName)){
  					StringTokenizer stk = new StringTokenizer(content.toString()," ");
  					stepPostp.putStepValue("ChangeLoadFactor",new Double(Double.parseDouble(stk.nextToken())));
  				}
  				
  				else if ("ChangeTolerance".equals(qName)){
  				}
  				
  				else if ("ChangeNumMaxIterations".equals(qName)){
  					StringTokenizer stk = new StringTokenizer(content.toString()," ");
  					stepPostp.putStepValue("ChangeNumMaxIterations",new Double(Double.parseDouble(stk.nextToken())));
  				}
  				
  				else if ("StepNumber".equals(qName)){
  					StringTokenizer stk = new StringTokenizer(content.toString()," ");
  					stepPostp.setLabel(stk.nextToken());
  				}
  				
  				else if ("FinalLoadFactor".equals(qName)){
  					StringTokenizer stk = new StringTokenizer(content.toString()," ");
  					stepPostp.putStepValue("FinalLoadFactor",new Double(Double.parseDouble(stk.nextToken())));
  				}
  				
  				
  				else if ("Model".equals(qName)){
  				}
  				
  				else if ("ProblemDriver".equals(qName)){
  				}
  				
  				else if ("GlobalAnalysisModel".equals(qName)){
  				}
  				
  				else if ("MaterialList".equals(qName)){
  				}
  				
  				else if ("Material".equals(qName)){
  				}
  				
  				else if ("Poisson".equals(qName)){
  				}
  				
  				else if ("Fc".equals(qName)){
  				}
  				
  				else if ("Ft".equals(qName)){
  				}
  				
  				else if ("ec".equals(qName)){
  				}
  				
  				else if ("et".equals(qName)){
  				}
  				
  				
  				else if ("E0".equals(qName)){
  				}
  				
  				else if ("Elasticity".equals(qName)){
  				}
  				
  				else if ("ShearModulus".equals(qName)){
  				}
  				
  				else if ("Yielding".equals(qName)){
  				}
  				
  				else if ("NodeList".equals(qName)){
  		
  				}
  				
  				else if ("Node".equals(qName)){
  					if(!checkPoint(this.point,this.pointsList)){
  						this.pointsList.add(this.point);
  					}
  				}
  				
  				else if ("NodeValues".equals(qName)){
  				}
  				
  				else if ("Coord".equals(qName)){
//  			    StringTokenizer � apenas um quebrador de strings
  				    StringTokenizer stk = new StringTokenizer(content.toString()," ");
  					coord.x = Double.parseDouble(stk.nextToken());
  					coord.y = Double.parseDouble(stk.nextToken());
  					coord.z = Double.parseDouble(stk.nextToken());
  					point.setCoord(coord);
  				}
  				
  				else if ("Restraints".equals(qName)){
  					
  				}
  				
  				else if ("DOFLabels".equals(qName)){
  					StringTokenizer stk = new StringTokenizer(content.toString()," ");
  					int cont = stk.countTokens();
  					for(int i=0; i<cont;i++){
  						dofLabels.add((String)stk.nextToken());
  					}
  				}
  				
  				else if ("Displacements".equals(qName)){
  					StringTokenizer stk = new StringTokenizer(content.toString()," ");
  					int cont = stk.countTokens();
  					for(int i=0; i<cont;i++){
  						point.putValue((String)dofLabels.get(i),new Double(Double.parseDouble(stk.nextToken())));
  					}
  				}
  				else if ("ElementList".equals(qName)){
  					
  				}
  				
  				else if ("Element".equals(qName)){
  					patchesList.add(patch);
  				}
  				
  				else if ("Incidence".equals(qName)){
  					StringTokenizer stk = new StringTokenizer(content.toString()," ");
  				    int cont = stk.countTokens();
  					String[] patchPoints = new String[cont];
  					for(int i=0;i<cont;i++){
  						patchPoints[i] = stk.nextToken();
  					}
  					patch.setPointsLabels(patchPoints);
  				}
  				
  				else if ("AnalysisModel".equals(qName)){
  				}
  				
  				else if ("IntegrationOrder".equals(qName)){
  				}
  				
  				else if ("ElmCrossSections".equals(qName)){
  				}
  				
  				else if ("DegenerationList".equals(qName)){
  				}
  				
  				else if ("Degeneration".equals(qName)){
  				}
  				
  				else if ("GeneralizedStrains".equals(qName)){
  				}
  				
  				else if ("GeneralizedStress".equals(qName)){
  				}
  				
  				else if ("SectionPoint".equals(qName)){
  				}
  				
  				else if ("PointStrains".equals(qName)){
  				}
  				
  				else if ("PointStress".equals(qName)){
  				}
  				
  				
  				 if ("PostpData".equals(qName)){
  				//	 stepPostp.setPoints(pointsList);
  				//	 stepPostp.setBoundaries(boundariesList);
  				//	 stepPostp.setPatches(patchesList);
  				 }
                  
//					Read "</PointsList>"
  				else if ("PointsList".equals(qName)){
  					
  				}
                  
//  				Read "</PointModel>"
  				else if ("PointModel".equals(qName)){
  					if(!checkPoint(this.point,this.pointsList)){
  						this.pointsList.add(this.point);
  					}
  				}
  				
//  				Read "</coord>"
  				else if ("coord".equals(qName)){
  					//    StringTokenizer � apenas um quebrador de strings
  				    StringTokenizer stk = new StringTokenizer(content.toString()," ");
  					coord.x = Double.parseDouble(stk.nextToken());
  					coord.y = Double.parseDouble(stk.nextToken());
  					coord.z = Double.parseDouble(stk.nextToken());
  					point.setCoord(coord);
  				}
  				
//  				Read "</value>"
  				else if ("value".equals(qName)){
  					StringTokenizer stk = new StringTokenizer(content.toString()," ");
  					point.putValue(stk.nextToken(),new Double(Double.parseDouble(stk.nextToken())));
  				}

//                Read "</PatchesList ...>"
                  else if ("PatchesList".equals(qName)){
               	   
                  }
                  
//					Read "</Patch ...>"
  				else if ("Patch".equals(qName)){
  					patchesList.add(patch);
  				}
  				
//  				Read "</patchPointsLabels ...>"
  				else if ("patchPointsLabels".equals(qName)){
  					StringTokenizer stk = new StringTokenizer(content.toString()," ");
  				    int cont = stk.countTokens();
  					String[] patchPoints = new String[cont];
  					for(int i=0;i<cont;i++){
  						patchPoints[i] = stk.nextToken();
  					}
  					patch.setPointsLabels(patchPoints);
  				}
                  
//                Read "</BoundariesList ...>"
                  else if ("BoundariesList".equals(qName)){
               	   
                  }
                 
//                Read "</Boundary ...>"
  				else if ("Boundary".equals(qName)){
  					boundariesList.add(boundary);
  				}
  				
//  				Read "<boundPointsLabels ...>"
  				else if ("boundPointsLabels".equals(qName)){
  					StringTokenizer stk = new StringTokenizer(content.toString()," ");
  				    int cont = stk.countTokens();
  					String[] boundaryPoints = new String[cont];
  					for(int i=0;i<cont;i++){
  						boundaryPoints[i] = stk.nextToken();
  					}
  					boundary.setPointsLabels(boundaryPoints);
  				}
  				else if ("Step".equals(qName)){
                  
  				}
 			
// 				Read "</stepAttributes>"
 				 	else if ("stepAttributes".equals(qName))
 				 	{
 				 		StringTokenizer stk = new StringTokenizer(content.toString()," ");
 				 		stepPostp.putStepValue(stk.nextToken(),new Double(Double.parseDouble(stk.nextToken())));
 				 	}
  	
               content = new StringBuffer();
                  
  			}//END OF ENDELEMENT*********************************************
  			
  		});//END OF PARSE*******************************************************
  		
  	}//END OF TRY*******************************************************************
  		
  	catch (ParserConfigurationException pce){
  		System.out.println("Could not create that parser.");
  		System.out.println(pce.getMessage());
  		
  		//System.exit(1);
  	}
  	catch (SAXException se){
  		System.out.println("Problem with the SAX parser.");
  		System.out.println(se.getMessage());
  		//System.exit(2);
  	}
  	catch (IOException ioe){
  		System.out.println("Error reading file.");
  		System.out.println(ioe.getMessage());
  		this.isValid = false;
  		//System.exit(3);
  	}
  	
  }//END OF FILLMODELFROMFILE**********************************************************
  

  /* (non-Javadoc)
   * @see br.ufmg.dees.insane.persistence.Persistence#fillFileFromModel(java.lang.String)
   */
  public void fillFileFromModel() {
  	
  	try{
			DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			Document doc = builder.newDocument();
			
//Creating formator
			DecimalFormat fmt = new DecimalFormat();
			DecimalFormatSymbols fsym = new DecimalFormatSymbols();
			fsym.setDecimalSeparator('.');
			fmt.setDecimalFormatSymbols(fsym);
			
			//All the classes which will be instantiated are declared here
          
//        Creating root DOM element "PostpModel"
			org.w3c.dom.Element root = doc.createElement("PostpData");
			doc.appendChild(root);
			
//			Creating DOM element "PointsList"
			org.w3c.dom.Element elmPointsList = doc.createElement("PointsList");
			root.appendChild(elmPointsList);
			
//			Creating DOM elements "PointModel"
			/*ListIterator points = stepPostp.getPoints().listIterator();
			while (points.hasNext()){
				point = (PointModel)points.next();
				org.w3c.dom.Element elmPointModel = doc.createElement("PointModel");
				elmPointModel.setAttribute("label", "" + point.getLabel());
				elmPointsList.appendChild(elmPointModel);
				
				//Creating DOM elements "coord"
				org.w3c.dom.Element elmCoord = doc.createElement("coord");
				fmt.applyPattern("0.0000E00");
				elmCoord.appendChild(doc.createTextNode(String.valueOf(fmt.format(point.getX()))));
				elmCoord.appendChild(doc.createTextNode(" "));
				elmCoord.appendChild(doc.createTextNode(String.valueOf(fmt.format(point.getY()))));
				elmCoord.appendChild(doc.createTextNode(" "));
				elmCoord.appendChild(doc.createTextNode(String.valueOf(fmt.format(point.getZ()))));
				elmPointModel.appendChild(elmCoord);
				
				//Creating DOM elements "value"
					for(int j=0;j<point.getKeys().size();j++){
						org.w3c.dom.Element elmValue = doc.createElement("value");
						elmValue.appendChild(doc.createTextNode((String)point.getKeys().get(j)));
						elmValue.appendChild(doc.createTextNode(" "));
						elmValue.appendChild(doc.createTextNode(String.valueOf(fmt.format(point.getValue((String)point.getKeys().get(j))))));
						elmValue.appendChild(doc.createTextNode(" "));
						elmPointModel.appendChild(elmValue);
					}
			}
*/
			//Creating DOM element "PatchesList"
			org.w3c.dom.Element elmPatchesList = doc.createElement("PatchesList");
			root.appendChild(elmPatchesList);

			//Creating DOM elements "Patch"
	/*		ListIterator patches = stepPostp.getPatches().listIterator();
			while (patches.hasNext()){
				patch = (Patch)patches.next();
				org.w3c.dom.Element elmPatch = doc.createElement("Patch");
				elmPatch.setAttribute("label", "" + patch.getLabel());
				elmPatchesList.appendChild(elmPatch);
				
				//Creating DOM elements "patchPointsLabels"
				org.w3c.dom.Element elmPatchPointsLabels = doc.createElement("patchPointsLabels");
				for(int i=0;i<patch.getPointsLabels().length;i++){
					elmPatchPointsLabels.appendChild(doc.createTextNode(patch.getPointsLabels()[i]));
					elmPatchPointsLabels.appendChild(doc.createTextNode(" "));
					elmPatch.appendChild(elmPatchPointsLabels);
				}

			}//End of creating DOM element "Patch"	
	*/		
//			Creating DOM element "BoundariesList"
			org.w3c.dom.Element elmBoundariesList = doc.createElement("BoundariesList");
			root.appendChild(elmBoundariesList);

			//Creating DOM elements "Boundary"
	/*		ListIterator boundaries =stepPostp.getBoundaries().listIterator();
			while (boundaries.hasNext()){
				boundary = (Boundary)boundaries.next();
				org.w3c.dom.Element elmBoundary = doc.createElement("Boundary");
				elmBoundary.setAttribute("label", "" + boundary.getLabel());
				elmBoundariesList.appendChild(elmBoundary);
				
				//Creating DOM elements "boundPointsLabels"
				org.w3c.dom.Element elmBoundPointsLabels = doc.createElement("boundPointsLabels");
				for(int i=0;i<boundary.getPointsLabels().length;i++){
					elmBoundPointsLabels.appendChild(doc.createTextNode(boundary.getPointsLabels()[i]));
					elmBoundPointsLabels.appendChild(doc.createTextNode(" "));
					elmBoundary.appendChild(elmBoundPointsLabels);
				}

			}//End of creating DOM element "Boundary"
	*/		
//        Creating root DOM element "Step"
			org.w3c.dom.Element elemStep = doc.createElement("Step");
			root.appendChild(elemStep);
			elemStep.setAttribute("label", "" + stepPostp.getLabel());
			//Creating DOM elements "stepAttributes"
			
			for(int j=0;j<stepPostp.getKeys().size();j++){
				org.w3c.dom.Element elmStepAttributes = doc.createElement("stepAttributes");
				elmStepAttributes.appendChild(doc.createTextNode((String)stepPostp.getKeys().get(j)));
				elmStepAttributes.appendChild(doc.createTextNode(" "));
				elmStepAttributes.appendChild(doc.createTextNode(String.valueOf(fmt.format(stepPostp.getStepValue((String)stepPostp.getKeys().get(j))))));
				elmStepAttributes.appendChild(doc.createTextNode(" "));
				elemStep.appendChild(elmStepAttributes);
			}
			
			docOutput = doc;
	
		} //end of try 
  	
  	catch (ParserConfigurationException pce){
  		System.out.println("Could not create DOM parser.");
  		System.out.println(pce.getMessage());
  		System.exit(4);
  	}
  	
	}
  
  /** Serializes the DOM tree into a XML file.
	*@param resultFile The name of the output file.
	*/
	public void serializeDOMTreeIntoXMLDocument(String resultFile)
	{
		
		try
		{
			Transformer trans = TransformerFactory.newInstance().newTransformer();
			trans.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "postp.dtd");
			trans.setOutputProperty(OutputKeys.INDENT, "yes");
			trans.setOutputProperty(OutputKeys.ENCODING, "ISO8859-1");
			trans.setOutputProperty(OutputKeys.INDENT, "yes");
			trans.transform(new DOMSource((Document) docOutput ), new StreamResult(resultFile));
		} 
		catch (TransformerException te)
		{
			System.out.println("Problem serializing DOM tree");
			System.out.println(te.getMessageAndLocation());
			System.exit(5);
		}
	}

  /* (non-Javadoc)
   * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
   */
  public void printFile() {
  	 File newdir0 = new File("RESULTS");
       if (newdir0.mkdir());
       File newdir1;
       if(newdir0.getAbsolutePath()+File.separator+stepPostp.getLabel()==null){
      	 newdir1 = new File(newdir0.getAbsolutePath()+File.separator+stepPostp.getLabel());
       }
       newdir1 = new File(newdir0.getAbsolutePath()+File.separator+stepPostp.getLabel());
       if (newdir1.mkdir());
       File newdir = new File(newdir1.getAbsolutePath()+File.separator+"XMLs");
       if (newdir.mkdir());
       this.fillFileFromModel();
       
           File newdir2 = new File(newdir1.getAbsolutePath()+File.separator+"XMLs");
           if (newdir2.mkdir());
           this.fillFileFromModel();
           this.serializeDOMTreeIntoXMLDocument(newdir.getAbsolutePath()+File.separator+"Result-"+stepPostp.getLabel()+".xml");
       
   
  }
  int cont = 1 ;
  public void update(Observable arg0, Object arg) {
  	String label = stepPostp.getLabel();
  	stepPostp = (((StepPostp)((FemModelToPostpModel)arg0).getStepPostp()));
  	stepPostp.setLabel(label);

  	File newdir0 = new File("RESULTS");
      if (newdir0.mkdir());
      File newdir1 = new File(newdir0.getAbsolutePath()+File.separator+stepPostp.getLabel());
      if (newdir1.mkdir());
      File newdir = new File(newdir1.getAbsolutePath()+File.separator+"XMLs");
      if (newdir.mkdir());
      this.fillFileFromModel();

      DecimalFormat fmt = new DecimalFormat();
      fmt.applyPattern("000");
      this.serializeDOMTreeIntoXMLDocument(newdir.getAbsolutePath()+File.separator+stepPostp.getLabel()+"-STEP-"+fmt.format(cont)+".xml");
      
      cont++;
 
  }

	public boolean checkPoint(PointModel point, ArrayList pointList){
		boolean compare = false;
		ListIterator itr = pointList.listIterator();	
		while(itr.hasNext()){
			PointModel p = (PointModel)itr.next();
			if(p.getLabel().equals(point.getLabel())){
				compare = true;
				return compare;
			}else{
				 continue;
			}
		}
	return compare;
	}
	
	public PointModel searchElementNode(PointModel point, ArrayList pointList){
		PointModel p = new PointModel();
		ListIterator itr = pointList.listIterator();	
		while(itr.hasNext()){
			p = (PointModel)itr.next();
			if(p.getLabel().equals(point.getLabel())){
				return  p;
			 }else{
				 continue;
			 }
		}
	return null;
	}
  
  
	/**
	 * The method return the isValid.
	 * @return Returns The isValid.
	 */
	public boolean isValid() {
		return isValid;
	}

	/**
	 * The method set the field isValid from the parameter isValid.
	 * @param isValid The isValid to set.
	 */
	public void setValid(boolean isValid) {
		this.isValid = isValid;
	}

//****************************************************************************************************************************	
}//end of this Class
